﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace MOJsdk
{
    public class MOJFile : MOJBasicFileClass
    {
        public MOJFile(MOJBasicFileClass file)
        {
            INI(file);
        }
        public MOJFile(int FILEID)
        {
            INI(GlobalVars.TheHost.GetFile(FILEID));
        }
        private void INI(MOJBasicFileClass file)
        {
            this.Details = file.Details;
            this.DiskID = file.DiskID;
            this.FileID = file.FileID;
            this.FileIndex = file.FileIndex;
            this.FilePath = file.FilePath;
            this.FileType = file.FileType;
            this.ISO639_Code = file.ISO639_Code;
            this.MovieCode = file.MovieCode;
        }

        public string GetOrganizedPath()
        {
            MOJOrganizeRules rules = new MOJOrganizeRules();
            string path;

            if (rules.KeepFileDisk)
                path = GlobalVars.TheHost.DrivePool[this.DiskID] + @":\";
            else
                path = GlobalVars.TheHost.DrivePool[rules.DiskID] + @":\";

            return Path.Combine(path, rules.GetFolderName(this), rules.GetFileName(this));
        }

        public bool IsOrganized
        {
            get
            {
                return this.FileFullPath.ToLower() == GetOrganizedPath().ToLower();
            }
        }
        public MOJMovie Movie
        {
            get { return new MOJMovie(this.MovieCode); }
        }
        public ISO639_1 Language
        {
            get
            {
                if (ISO639_Code == 0)
                    return null;

                return ISO639_1.LoadLanguage(ISO639_Code);
            }
        }
    }

    public class MOJOrganizeRules
    {
        private IHost _host;
        private MojSystem _system;

        public MOJOrganizeRules()
        {
            _host = GlobalVars.TheHost;
            _system = GlobalVars.TheHost.SystemVars;
            INIVars();
        }

        private void INIVars()
        {
            if (!_system.VarExist("MojOrgDiskID"))
                _system.SetVar("MojOrgDiskID", "");

            if (!_system.VarExist("MojOrgFolderName"))
                _system.SetVar("MojOrgFolderName", "Movies/%title% (%year%)");

            if (!_system.VarExist("MojOrgFileName"))
                _system.SetVar("MojOrgFileName", "%title% (%year%)");

            if (!_system.VarExist("MojOrgNumber"))
                _system.SetVar("MojOrgNumber", "CD%index%");

            if (!_system.VarExist("MojOrgShowOnlyOnMulti"))
                _system.SetVar("MojOrgShowOnlyOnMulti", "true");

            if (!_system.VarExist("MojOrgKeepFileDisk"))
                _system.SetVar("MojOrgKeepFileDisk", "true");
        }

        public string DiskID
        { get { return _system.GetVar("MojOrgDiskID"); } }
        public string FolderName
        { get { return _system.GetVar("MojOrgFolderName"); } }
        public string FileName
        { get { return _system.GetVar("MojOrgFileName"); } }
        public string FileNumber
        { get { return _system.GetVar("MojOrgNumber"); } }
        public bool ShowFileNumberOnlyOnMultiMovies
        { get { return _system.GetVar("MojOrgShowOnlyOnMulti").ToBool(); } }
        public bool KeepFileDisk
        { get { return _system.GetVar("MojOrgKeepFileDisk").ToBool(); } }

        public string GetFileName(MOJFile file)
        {
            MOJMovie m = file.Movie;

            string name = MOJOrganizeRules.BuildName(file, FileName);

            if (!ShowFileNumberOnlyOnMultiMovies || m.MovieFiles.Count > 1)
            {
                name += "." + MOJOrganizeRules.BuildName(file, FileNumber);
            }
            if (file.FileType == 1)
                name += "." + file.Language.Country;

            name += Path.GetExtension(file.FilePath);

            return name;
        }
        public string GetFolderName(MOJFile file)
        {
            return MOJOrganizeRules.BuildName(file, FolderName);
        }

        public static string BuildName(MOJFile file, string query)
        {
            MOJMovie m = new MOJMovie(file.MovieCode);

            query = query.Replace("%title%", m.MovieTitle);
            query = query.Replace("%year%", m.MovieYear);
            query = query.Replace("%index%", file.FileIndex.ToString());

            return TOOLS.RemoveIlegalChars(query);
        }
    }
}
